package com.september.mongo.entities;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Reference;
import com.september.util.BaseData;

@Entity
public class City extends BaseData {

	/*
	 * The main 'geoname' table has the following fields :
	 * --------------------------------------------------- geonameid : integer
	 * id of record in geonames database name : name of geographical point
	 * (utf8) varchar(200) asciiname : name of geographical point in plain ascii
	 * characters, varchar(200) alternatenames : alternatenames, comma separated
	 * varchar(5000) latitude : latitude in decimal degrees (wgs84) longitude :
	 * longitude in decimal degrees (wgs84) feature class : see
	 * http://www.geonames.org/export/codes.html, char(1) feature code : see
	 * http://www.geonames.org/export/codes.html, varchar(10) country code :
	 * ISO-3166 2-letter country code, 2 characters cc2 : alternate country
	 * codes, comma separated, ISO-3166 2-letter country code, 60 characters
	 * admin1 code : fipscode (subject to change to iso code), see exceptions
	 * below, see file admin1Codes.txt for display names of this code;
	 * varchar(20) admin2 code : code for the second administrative division, a
	 * county in the US, see file admin2Codes.txt; varchar(80) admin3 code :
	 * code for third level administrative division, varchar(20) admin4 code :
	 * code for fourth level administrative division, varchar(20) population :
	 * bigint (8 byte int) elevation : in meters, integer dem : digital
	 * elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca
	 * 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm
	 * processed by cgiar/ciat. timezone : the timezone id (see file
	 * timeZone.txt) varchar(40) modification date : date of last modification
	 * in yyyy-MM-dd format
	 * 
	 * 
	 * AdminCodes: Most adm1 are FIPS codes. ISO codes are used for US, CH, BE
	 * and ME. UK and Greece are using an additional level between country and
	 * fips code.
	 */
	@Id
	private String geonameid;
	private String name;
	private String asciiname;
	private String alternatenames;
	private String latitude;
	private String longitude;
	private String feature_class;
	private String feature_code;
	private String country_code;
	private String cc2;
	private String admin1_code;
	private String admin2_code;
	private String admin3_code;
	private String admin4_code;
	private String population;
	private String elevation;
	private String dem;
	private String timezone;
	public Region getRegion() {
		return region;
	}

	public void setRegion(Region region) {
		this.region = region;
	}

	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}

	private String modification_date;
	
	@Reference
	private Region region;
	@Reference
	private Province province;
	@Reference
	private Nation nation;

	public Nation getNation() {
		return nation;
	}

	public void setNation(Nation nation) {
		this.nation = nation;
	}

	public String getGeonameid() {
		return geonameid;
	}

	public void setGeonameid(String geonameid) {
		this.geonameid = geonameid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAsciiname() {
		return asciiname;
	}

	public void setAsciiname(String asciiname) {
		this.asciiname = asciiname;
	}

	public String getAlternatenames() {
		return alternatenames;
	}

	public void setAlternatenames(String alternatenames) {
		this.alternatenames = alternatenames;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getFeature_class() {
		return feature_class;
	}

	public void setFeature_class(String feature_class) {
		this.feature_class = feature_class;
	}

	public String getFeature_code() {
		return feature_code;
	}

	public void setFeature_code(String feature_code) {
		this.feature_code = feature_code;
	}

	public String getCountry_code() {
		return country_code;
	}

	public void setCountry_code(String country_code) {
		this.country_code = country_code;
	}

	public String getCc2() {
		return cc2;
	}

	public void setCc2(String cc2) {
		this.cc2 = cc2;
	}

	public String getAdmin1_code() {
		return admin1_code;
	}

	public void setAdmin1_code(String admin1_code) {
		this.admin1_code = admin1_code;
	}

	public String getAdmin2_code() {
		return admin2_code;
	}

	public void setAdmin2_code(String admin2_code) {
		this.admin2_code = admin2_code;
	}

	public String getAdmin3_code() {
		return admin3_code;
	}

	public void setAdmin3_code(String admin3_code) {
		this.admin3_code = admin3_code;
	}

	public String getAdmin4_code() {
		return admin4_code;
	}

	public void setAdmin4_code(String admin4_code) {
		this.admin4_code = admin4_code;
	}

	public String getPopulation() {
		return population;
	}

	public void setPopulation(String population) {
		this.population = population;
	}

	public String getElevation() {
		return elevation;
	}

	public void setElevation(String elevation) {
		this.elevation = elevation;
	}

	public String getDem() {
		return dem;
	}

	public void setDem(String dem) {
		this.dem = dem;
	}

	public String getTimezone() {
		return timezone;
	}

	public void setTimezone(String timezone) {
		this.timezone = timezone;
	}

	public String getModification_date() {
		return modification_date;
	}

	public void setModification_date(String modification_date) {
		this.modification_date = modification_date;
	}

	@Override
	public String[] getOrdreredField() {
		// TODO Auto-generated method stub
		return new String[] { "geonameid", "name", "asciiname",
				"alternatenames", "latitude", "longitude", "feature_class",
				"feature_code", "country_code", "cc2", "admin1_code",
				"admin2_code", "admin3_code", "admin4_code", "population",
				"elevation", "dem", "timezone", "modification_date" };
	}

	@Override
	public void manageRefernces() {
		// TODO Auto-generated method stub
		String regID = parseFields('.',this.getCountry_code(), this.getAdmin1_code());
		String provID = parseFields('.',this.getCountry_code(), this.getAdmin1_code(), this.getAdmin2_code());
		String natID = parseFields('.',this.getCountry_code());
		
		Datastore ds = getDs();
		
		Region reg = ds.find(Region.class).field("_id").equal(regID).get();
		Province prov = ds.find(Province.class).field("_id").equal(provID).get();
		Nation nat = ds.find(Nation.class).field("_id").equal(natID).get();
		
		this.setNation(nat);
		this.setProvince(prov);
		this.setRegion(reg);
		
	}

}
